package pl.vm.domain;

import pl.vm.domain.exceptions.InvalidPlayerException;
import pl.vm.domain.exceptions.InvalidStateException;
import pl.vm.domain.exceptions.NotEnoughFoundsException;
import pl.vm.domain.exceptions.NotEnoughSpaceException;
import pl.vm.domain.exceptions.PlayerAlreadyExistException;
import pl.vm.domain.exceptions.PlayerNotExistsException;
import pl.vm.domain.interfaces.TransferManager;
import pl.vm.utils.ObjectCloner;

public class TransferManagerImpl implements TransferManager {

	private Seazon seazon;
	
	public TransferManagerImpl(Seazon seazon) {
		this.seazon = seazon;
	}
	
	@Override
	public void playerOut(VirtualTeam team, Player player) throws PlayerNotExistsException, InvalidPlayerException, InvalidStateException {
		VirtualTeam newTeam = (VirtualTeam) ObjectCloner.deepCopy(team);
		newTeam.sellPlayer(player);
		seazon.getRoundManager().getCurrentRound().changeTeam(team, newTeam);
		seazon.getTeamManager().changeTeam(team, newTeam);
	}

	@Override
	public void playerIn(VirtualTeam team, Player player) throws NotEnoughSpaceException, PlayerAlreadyExistException, NotEnoughFoundsException, InvalidStateException {
		VirtualTeam newTeam = (VirtualTeam) ObjectCloner.deepCopy(team);
		newTeam.buyPlayer(player);
		seazon.getRoundManager().getCurrentRound().changeTeam(team, newTeam);
		seazon.getTeamManager().changeTeam(team, newTeam);
		if (newTeam.isTeamRegistered()) {
			seazon.getRoundManager().getCurrentRound().getClassification().substractPointForTeam(newTeam, TransferManager.TRANSFER_PENALTY_POINTS);
		}
	}

}
